package gogohome.ourhome;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class socket_client extends Thread{
	 DataInputStream read;
	 DataOutputStream write;
	private Socket socket;
	Handler mHandler;
	byte[] line = new byte[50];
	int b;
	
	socket_client(Handler handler){
		mHandler = handler;
	}
	
     
	//private Thread checkUpdate = new Thread() {
	        public void run() {
	            try {
	                //String line;
	            	Message msg = Message.obtain(mHandler);
	                Log.w("ChattingStart", "Start Thread");
//	                while (true) {
	                while (true) {
	                    //Log.w("Chatting is running", "chatting is running");
//	                    read.read(line);
	                    //if(read.read(line) >0){
	                		if((b = read.read())>0){
	                			read.read(line);
	                			msg.what = 0;
	                			mHandler.sendMessage(msg);
	                			
	                		}
	                    	 
		                    //line = networkReader.readLine().getBytes();
		       //             int type = line[4];
		                    //mHandler.post(showUpdate);
		            //        if(type==1){
		              //      	mHandler.sendEmptyMessage(1);	
		                //    }
	                    	
	                    }
	                    
	                
	 
	            } catch (Exception e) {
	            }
	        }
	  //  };
		/*
	    public Runnable showUpdate = new Runnable() {
	        public void run() {
	        	Message msg = Message.obtain();
	        
	        //	msg.obj = line;
	        	if(line[4]==1){
	        		msg.what=1;
	        	}
	        	
	//            Toast.makeText(clint_android.this, "Coming word: " + html[0],
	  //                  Toast.LENGTH_SHORT).show();
	        //    byte[] a = html;
	    //        historyMsg += a[0]+a[1] +"2"+ "\n";
	      //      mResult.setText(historyMsg);
	            //os.close();
	            //is.close();
	            
	        }
	    };*/

	public void setSocket(String ip, int port) throws IOException {
        try {
        	//PacketData msg = new PacketData();
        	socket = new Socket(ip, port);
           /*
        	OutputStream os = socket.getOutputStream();
           InputStream is = socket.getInputStream();
           */
        	read = new DataInputStream(socket.getInputStream());
        	write = new DataOutputStream(socket.getOutputStream());        	
        	
    //       Toast.makeText(clint_android.this, "Connection is Success",Toast.LENGTH_SHORT).show();
           

        } catch (IOException e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }
	public byte[] read_data(){
		
		return line; 
	}
	public void clint_certfi(){
		
	}

	public void write_data(byte[] packet_data) throws IOException {
		// TODO Auto-generated method stub
		//byte[] a = {1,1,1,1,1,1,1,1,1};
		write.write(packet_data);
		write.flush();
		//write.close();
		//read.close();
	//	socket.close();
		
	}

}
